# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

NACL_SDK_ROOT ?= $(abspath $(CURDIR)/../..)
include $(NACL_SDK_ROOT)/tools/common.mk

TARGET = lua
LIBS = lua ppapi_simple tar
NACL_LDFLAGS += $(NACL_CLI_MAIN_LIB)
PNACL_LDFLAGS += $(NACL_CLI_MAIN_LIB)

ifeq ($(LUA_NO_READLINE),)
LIBS += readline ncurses
CFLAGS += -DLUA_USE_READLINE
endif

LIBS += nacl_io ppapi_cpp ppapi
ifeq ($(TOOLCHAIN),glibc)
LIBS += util dl
NACL_LDFLAGS += -Wl,-export-dynamic
endif
SOURCES = lua.c main.c
INSTALL_DIR = $(NACL_PACKAGES_PUBLISH)/lua/$(TOOLCHAIN)
CFLAGS += -DLUA_USE_POSIX


ifeq ($(TOOLCHAIN),pnacl)
EXEEXT=.pexe
else
EXEEXT=.nexe
endif

ifneq ($(TOOLCHAIN),pnacl)
# We want the nmf to contain all the .nexe we have previously
# built as well as the one currently being built, so we modify
# the list of executables that we pass to create_nmf.
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_x86_64$(EXEEXT))
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_x86_32$(EXEEXT))
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_arm$(EXEEXT))
endif

# Build rules generated by macros from common.mk:

$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS))))

ifeq ($(CONFIG),Release)
$(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS)))
$(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped))
else
$(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS)))
endif

$(eval $(call NMF_RULE,$(TARGET),))

CHROMEAPPS = $(NACL_SRC)/third_party/libapps/
LIB_DOT = $(CHROMEAPPS)/libdot
NASSH = $(CHROMEAPPS)/nassh

MIRROR_URL=http://storage.googleapis.com/nativeclient-mirror/nacl

# Mirrored from http://luajit.org/download/scimark.lua
SCIMARK=scimark.lua
# Mirrored from http://kripken.github.io/lua.vm.js/binarytrees.lua
BINARYTREES=binarytrees.lua
# Mirrored from http://www.lua.org/tests/5.2/lua-5.2.2-tests.tar.gz
LUA_TESTS_FILE=lua-5.2.2-tests.tar.gz

$(OUTBASE)/$(LUA_TESTS_FILE):
	cd $(OUTBASE) && curl -O --progress-bar $(MIRROR_URL)/$(LUA_TESTS_FILE)

$(OUTBASE)/$(SCIMARK):
	cd $(OUTBASE) && curl -O --progress-bar $(MIRROR_URL)/$(SCIMARK)

$(OUTBASE)/$(BINARYTREES):
	cd $(OUTBASE) && curl -O --progress-bar $(MIRROR_URL)/$(BINARYTREES)

install-tests: $(OUTBASE)/$(LUA_TESTS_FILE) $(OUTBASE)/$(SCIMARK) $(OUTBASE)/$(BINARYTREES)
	tar -C $(OUTBASE) -xf $(OUTBASE)/$(LUA_TESTS_FILE)
	cd $(OUTBASE)/lua-5.2.2-tests && patch -p1 < $(CURDIR)/lua_tests.patch
	mkdir -p $(INSTALL_DIR)
	tar -c -f $(INSTALL_DIR)/luadata.tar -C $(OUTBASE) lua-5.2.2-tests
	cp $(OUTBASE)/$(SCIMARK) $(OUTBASE)/$(BINARYTREES) $(INSTALL_DIR)

install: install-tests
	../../build_tools/template_expand.py manifest.json version=$(NACLPORTS_REVISION) > $(INSTALL_DIR)/manifest.json
	LIBDOT_SEARCH_PATH=$(CHROMEAPPS) $(LIB_DOT)/bin/concat.sh -i $(NASSH)/concat/nassh_deps.concat -o $(INSTALL_DIR)/hterm.concat.js
	cp $(OUTDIR)/lua*$(NACL_EXEEXT) $(INSTALL_DIR)
	rm -f $(INSTALL_DIR)/*_unstripped*$(EXEEXT)
	cp $(OUTDIR)/lua.nmf $(INSTALL_DIR)
	cp index.html $(INSTALL_DIR)
	cp lua.html $(INSTALL_DIR)
	cp lua.js $(INSTALL_DIR)
	cp ../../build_tools/pipeserver.js $(INSTALL_DIR)
	cp ../../build_tools/naclprocess.js $(INSTALL_DIR)
	cp ../../build_tools/naclterm.js $(INSTALL_DIR)
	cp background.js ${INSTALL_DIR}
	cp icon_16.png ${INSTALL_DIR}
	cp icon_48.png ${INSTALL_DIR}
	cp icon_128.png ${INSTALL_DIR}
ifeq ($(TOOLCHAIN),pnacl)
	sed -i.bak 's/x-nacl/x-pnacl/g' $(INSTALL_DIR)/naclprocess.js
endif
ifeq ($(TOOLCHAIN),glibc)
	cp -r $(OUTDIR)/lib* $(INSTALL_DIR)
endif
	cd $(INSTALL_DIR) && rm -f lua.zip && zip -r lua.zip .
